﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;

namespace PMS.Web
{
    public partial class AddUser : System.Web.UI.Page
    {
        BLL.PMS_UserBLL bll = new BLL.PMS_UserBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string roleID = Request["roleID"];
                string userID = Request["userID"];
                if (!string.IsNullOrEmpty(userID) && userID != "0")
                {
                    PMS_User model = bll.GetModel(Convert.ToInt32(userID));
                    if (ddl_Role.Items.FindByText(model.role) != null)
                    {
                        ddl_Role.Items.FindByText(model.role).Selected = true;
                    }
                    txt_UserName.Text = model.loginName;
                    txt_PassWord.Text = model.loginPassword;
                    hid_userID.Value = userID;
                }
                if (!string.IsNullOrEmpty(roleID) && roleID != "0")
                {
                    hid_RoleID.Value = roleID;
                }
            }
        }


        protected void btn_Add_Click(object sender, EventArgs e)
        {
            PMS_User model = new PMS_User();
            model.id = Convert.ToInt32(hid_userID.Value);
            model.roleID = Convert.ToInt32(hid_RoleID.Value);
            model.loginName = txt_UserName.Text;
            model.loginPassword = txt_PassWord.Text;
            model.role = ddl_Role.Text;
            if (model.id <= 0 && bll.Exite(model.loginName))
            {
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('用户名已存在');</script>");
                return;
            }
            if (bll.AddUpdate(model))
            {
                PageComm.GetCacheListLoad();
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>closeWin();</script>");
            }
        }
    }
}